package com.self.raft;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Peer {

    int id;

    /**
     * 请求队列(接收到其他peer的投票请求)
     */
    BlockingQueue<Vote> requestQueue = new LinkedBlockingQueue<>(20000);

    /**
     * 角色: -1:candidate 0:follower, 1: leader
     */
    volatile int role;

    volatile int currentLeader;

    volatile int currentEpoch;

    private Election election;

    List<Peer> allPeerList;

    public Peer(int id,List<Peer> allPeerList) {
        this.id = id;
        this.role = 0;
        this.currentEpoch = 0;
        this.currentLeader = this.id;
        this.allPeerList = allPeerList;
    }

    public void start() {
        election = new Election(this,this.allPeerList);
        election.start();
    }

    public void receiveVote(Vote vote) {
        this.requestQueue.offer(vote);
    }



}
